package com.grandtech.insurance.common;

import com.alibaba.fastjson.JSONObject;
import com.grandtech.insurance.domain.DbKhxx;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class HttpClient {

    public String postHttpClient(String url,String json){
        // 创建连接池
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        //ResponseHandler<String> responseHandler = new BasicResponseHandler();
        // 声明呀一个字符串用来存储response
        String result;
        // 创建httppost对象
        HttpPost httpPost = new HttpPost(url);
        // 给httppost对象设置json格式的参数
        StringEntity httpEntity = new StringEntity(json,"utf-8");
        // 设置请求格式
        httpPost.setHeader("Content-type","application/json");
        httpPost.setHeader("Authorization","eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhZG1pbiIsImNvbnRleHRVc2VySWQiOiIwIiwiY29udGV4dE5hbWUiOiLotoXnuqfnrqHnkIblkZgiLCJjb250ZXh0RGVwdElkIjoiMCIsImNvbnRleHRBcHBsaWNhdGlvbklkIjoiMCIsImlhdCI6MTYwMDc2MDczNiwiZXhwIjo0NzU0MzYwNzM2fQ.6KleN4NxVHU6pVKhTbDstCzNLL9_kys7RZK827D05EY");
        // 传参
        httpPost.setEntity(httpEntity);

        // 发送请求，并获取返回值
        String s = "";
        CloseableHttpResponse response = null;
        try {
            response = closeableHttpClient.execute(httpPost);
            StatusLine status = response.getStatusLine();
            int state = status.getStatusCode();
            if (state == HttpStatus.SC_OK){
                HttpEntity responseEntity = response.getEntity();
                String jsonString = EntityUtils.toString(responseEntity);
                s = jsonString;
            }else{
                // 将response转成String并存储在result中
                result = response.toString();
                System.out.println(result);
                s = "error";
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            s = "error";
        }finally {
            //关闭response
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                closeableHttpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return s;
    }

    public String getHttpClient(String url,String json){
        String s = "";
        //1.创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //2.创建httpget对象，设置url地址
        HttpGet httpGet = new HttpGet(url+"?"+json);
        CloseableHttpResponse response = null;
        //在外面创建，为方便最后关闭
        try {
            //3.使用httpclient发起请求，获取response
            response = httpClient.execute(httpGet);
            //4.解析响应
            if (response.getStatusLine().getStatusCode() == 200){
                //状态码200为成功
                String content = EntityUtils.toString(response.getEntity(), "utf8");
                System.out.println(content);
                s = content;
            }else{
                s = "调用接口异常";
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
            s = "调用异常";
            System.out.println(s);
        }finally {
            //关闭response
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return s;

    }

    public static void main(String[] args){

        /*Map m = new HashMap();
        m.put("qhdm","41");
        m.put("zwdm","07");
        m.put("nf","2021");
        String json = JSONObject.toJSONString(m);
        String url = "https://api.agribigdata.com.cn/insurance-server-business-web/api/v1/nydb/fdozzjg";
        HttpClient httpClient = new HttpClient();
        System.out.println(httpClient.postHttpClient(url,json));*/

        HttpClient httpClient = new HttpClient();
        String s = httpClient.getHttpClient("http://gykj123.cn:8849/nydb-server/api/v1/sysdic/listxl","category=bcyz");
        System.out.println(s);
    }

}
















